﻿using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Net;
using System.Text;

namespace kwzx.Common
{
    [DebuggerStepThrough]
    public static class Log
    {
        /// <summary>
        /// 写入错误信息
        /// </summary>
        /// <param name="rem"></param>
        /// <param name="ex"></param>
        public static void WriteError(string rem, Exception ex)
        {
            string context = rem + "\r\n" + ex.Message + "\r\n" + ex.Source;
            WriteInfo(context);
        }

        /// <summary>
        /// 写入最基本的日志信息
        /// </summary>
        /// <param name="context"></param>
        public static void WriteInfo(string context)
        {
            lock (context)
            {
                try
                {
                    var fi = new FileInfo(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
                    string path = fi.DirectoryName + "\\Log\\";
                    if (!File.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    string path1 = path + DateTime.Now.ToString("yyyy_MM_dd") + ".log";
                    if (!File.Exists(path1))
                    {
                        FileStream fs1 = new FileStream(path1, FileMode.Create, FileAccess.Write);
                        fs1.Close();
                    }
                    File.AppendAllText(path1,
                                       string.Format("\r\n__start__\r\n{0}\r\n{1}\r\n___end___\r\n",
                                                     DateTime.Now.ToString(CultureInfo.InvariantCulture), context));



                }

                catch (Exception)
                {
                }
            }


        }



    
    }
}